const Sequelize = require('sequelize');
module.exports = function (sequelize, DataTypes) {
    return sequelize.define(
        't_dept',
        {
            id: {
                autoIncrement: true,
                type: DataTypes.BIGINT,
                allowNull: false,
                primaryKey: true,
                comment: '部门ID',
            },
            dept_name: {
                type: DataTypes.STRING(100),
                allowNull: false,
                comment: '部门名称',
            },
            dept_code: {
                type: DataTypes.STRING(50),
                allowNull: false,
                comment: '部门编码',
                unique: 'dept_code',
            },
            parent_id: {
                type: DataTypes.BIGINT,
                allowNull: true,
                comment: '父部门ID',
                references: {
                    model: 't_dept',
                    key: 'id',
                },
            },
            manager_id: {
                type: DataTypes.BIGINT,
                allowNull: true,
                comment: '部门经理ID（后续关联t_employee）',
                references: {
                    model: 't_employee',
                    key: 'id',
                },
            },
            sort: {
                type: DataTypes.INTEGER,
                allowNull: true,
                defaultValue: 0,
                comment: '排序',
            },
            status: {
                type: DataTypes.TINYINT,
                allowNull: false,
                defaultValue: 1,
                comment: '状态：1-启用，0-停用',
            },
            create_time: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
            },
            update_time: {
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: Sequelize.Sequelize.literal('CURRENT_TIMESTAMP'),
            },
        },
        {
            sequelize,
            tableName: 't_dept',
            timestamps: false,
            indexes: [
                {
                    name: 'PRIMARY',
                    unique: true,
                    using: 'BTREE',
                    fields: [{ name: 'id' }],
                },
                {
                    name: 'dept_code',
                    unique: true,
                    using: 'BTREE',
                    fields: [{ name: 'dept_code' }],
                },
                {
                    name: 'idx_parent',
                    using: 'BTREE',
                    fields: [{ name: 'parent_id' }],
                },
                {
                    name: 'manager_id',
                    using: 'BTREE',
                    fields: [{ name: 'manager_id' }],
                },
            ],
        },
    );
};
